package com.asnike.selector{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class CampItemList extends Sprite{
		
		private const ITEM_START_POS_Y:Number = 20;
		private const ITEM_GAP_WIDTH:Number = 126;
		private const ITEM_GAP_HEIGHT:Number = 173;
		
		private var _items:Array = [];
		private var _itemClickCallback:Function;
		private var _category:TextField;
		
		public function CampItemList(){
			super();
			initCategory();
		}
		private function initCategory():void{
			var format:TextFormat;

			format = new TextFormat( 'ygo340', 14 );
//			format.color = 0xffffff;
			
			_category = new TextField;
			_category.text = '';
			_category.defaultTextFormat = format;
			_category.embedFonts = true;
			addChild( _category );
		}
		
		internal function addItem( $name:String, $price:int, $imageURL:String = '' ):void{
			setItem( $name, $price, $imageURL );
			showItems();
		}
		private function setItem( $name:String, $price:int, $imageURL:String ):void{
			var item:CampItem;
			
			item = new CampItem;
			item.setItemName( $name );
			item.setPrice( $price );
			item.setImage( $imageURL );
			item.addEventListener( MouseEvent.CLICK, itemClick );
			
			_items[_items.length] = item;
		}
		private function itemClick( $e:MouseEvent ):void{
			var item:CampItem;
			
			if( _itemClickCallback is Function ){
				item = $e.currentTarget as CampItem;
				_itemClickCallback( item.getName(), item.getPrice() );
			}
		}
		private function showItems():void{
			var item:CampItem, index:int;
			
			for each( item in _items ){
				item.x = ITEM_GAP_WIDTH*(index%3);
				item.y = ITEM_START_POS_Y + ITEM_GAP_HEIGHT*int( index/3 );
				addChild( item );
				++index;
			}
		}
		
		internal function removeItem( $target:CampItemBar ):void{
			_items.splice( _items.indexOf( $target ), 1 );
			showItems();
		}
		
		internal function setItemClickCallback( $function:Function ):void{
			_itemClickCallback = $function;
		}
		
		internal function setCategory( $name:String ):void{
			_category.text = $name;
		}
	}
}